/***************************************************************************//**
 * @file FDM/FDM_Manager.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef FDM_MANAGER_CPP
#define FDM_MANAGER_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <FDM/FDM_Manager.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

FDM_Manager::FDM_Manager() :
    m_fdm ( 0 )
{}

////////////////////////////////////////////////////////////////////////////////

FDM_Manager::~FDM_Manager()
{
    if ( m_fdm ) delete m_fdm; m_fdm = 0;
}

////////////////////////////////////////////////////////////////////////////////

void FDM_Manager::update( double timeStep, const FDM_DataInp &dataInp, FDM_DataOut &dataOut )
{
    determineInternalPhase( dataInp.inpPhase );

    switch ( m_inpPhase )
    {
    case PhaseIdle:
        m_outState = StateIdle;
        break;

    case PhaseInit:
        if ( !m_fdm ) m_fdm = new FDM_Main();
        if ( m_outState != StateReady ) m_fdm->init( dataInp, dataOut );
        m_outState = StateReady;    // ??????????????????????????????????????????????????
        break;

    case PhaseRun:
        m_fdm->update( timeStep, dataInp, dataOut );
        m_outState = StateRunning;  // ??????????????????????????????????????????????????
        break;

    case PhasePause:
        m_outState = StatePaused;   // ??????????????????????????????????????????????????
        break;

    case PhaseStop:
        if ( m_fdm ) delete m_fdm; m_fdm = 0;
        m_outState = StateStopped;  // ??????????????????????????????????????????????????
        break;
    }

    m_inpPhasePrev = m_inpPhase;
    m_outStatePrev = m_outState;
}

////////////////////////////////////////////////////////////////////////////////

void FDM_Manager::determineInternalPhase( MScSimPhase inpPhase )
{
    switch ( inpPhase )
    {
    case PhaseIdle:
        if ( m_outStatePrev == StateIdle || m_outStatePrev == StateStopped )
        {
            m_inpPhase = PhaseIdle;
        }
        else
        {
            m_inpPhase = PhaseStop;
        }
        break;

    case PhaseInit:
        if ( m_outStatePrev == StateIdle || m_outStatePrev == StateReady )
        {
            m_inpPhase = PhaseInit;
        }
        else
        {
            determineInternalPhase( PhaseIdle );
        }
        break;

    case PhaseRun:
        if ( m_outStatePrev == StateReady || m_outStatePrev == StatePaused )
        {
            m_inpPhase = PhaseRun;
        }
        else
        {
            determineInternalPhase( PhaseInit );
        }
        break;

    case PhasePause:
        if ( m_outStatePrev == StateReady || m_outStatePrev == StateRunning )
        {
            m_inpPhase = PhasePause;
        }
        else
        {
            m_inpPhase = m_inpPhasePrev;
        }
        break;

    case PhaseStop:
        m_inpPhase = PhaseStop;
        break;

    default:
        m_inpPhase = PhaseStop;
        break;
    }
}
